#include "StatusBar.h"
#include "ResourcesManager.h"
#include "PlayerManager.h"
#include "SDL2_gfxPrimitives.h"

void StatusBar::SetPosition(int x, int y)
{
    position.x = x, position.y = y;
}

void StatusBar::OnUpdate(SDL_Renderer *renderer, double coinNum)
{
    static TTF_Font* font = ResourcesManager::Instance().fontPool.find(ResID::Font_Main)->second;
    SDL_DestroyTexture(texTextBackground);
    texTextBackground = nullptr;
    SDL_DestroyTexture(texTextForeground);
    texTextForeground = nullptr;

    std::string str_val = std::to_string(coinNum);
    SDL_Surface *suf_text_background = TTF_RenderText_Blended(font, str_val.c_str(), colorTextBackground);
    SDL_Surface *suf_text_foreground = TTF_RenderText_Blended(font, str_val.c_str(), colorTextForeground);

    widthText = suf_text_background->w, heightText = suf_text_background->h;

    texTextBackground = SDL_CreateTextureFromSurface(renderer, suf_text_background);
    texTextForeground = SDL_CreateTextureFromSurface(renderer, suf_text_foreground);

    SDL_FreeSurface(suf_text_background);
    SDL_FreeSurface(suf_text_foreground);
}

void StatusBar::OnRender(SDL_Renderer *renderer, double currentHp)
{
    static SDL_Rect rectDst;
    static const ResourcesManager::TexturePool &texPool = ResourcesManager::Instance().texturePool;
    static SDL_Texture *texCoin = texPool.find(ResID::Tex_UICoin)->second;
    static SDL_Texture *texHeart = texPool.find(ResID::Tex_UIHeart)->second;
    static SDL_Texture *texHomeAvatar = texPool.find(ResID::Tex_UIHomeAvatar)->second;
    static SDL_Texture *texPlayerAvatar = texPool.find(ResID::Tex_UIPlayerAvatar)->second;

    rectDst.x = position.x, rectDst.y = position.y;
    rectDst.w = 78, rectDst.h = 78;
    SDL_RenderCopy(renderer, texHomeAvatar, nullptr, &rectDst);

    for (int i = 0; i < currentHp; i++) {
        rectDst.x = position.x + 78 + 15 + i * (32 + 2);
        rectDst.y = position.y;
        rectDst.w = 32, rectDst.h = 32;
        SDL_RenderCopy(renderer, texHeart, nullptr, &rectDst);
    }

    rectDst.x = position.x + 78 + 15;
    rectDst.y = position.y + 78 - 32;
    rectDst.w = 32, rectDst.h = 32;
    SDL_RenderCopy(renderer, texCoin, nullptr, &rectDst);

    rectDst.x += 32 + 10 + offsetShadow.x;
    rectDst.y = rectDst.y + (32 - heightText) / 2 + offsetShadow.y;
    rectDst.w = widthText, rectDst.h = heightText;
    SDL_RenderCopy(renderer, texTextBackground, nullptr, &rectDst);

    rectDst.x -= offsetShadow.x;
    rectDst.y -= offsetShadow.y;
    SDL_RenderCopy(renderer, texTextForeground, nullptr, &rectDst);

    rectDst.x = position.x + (78 - 65) / 2;
    rectDst.y = position.y + 78 + 5;
    rectDst.w = 65, rectDst.h = 65;
    SDL_RenderCopy(renderer, texPlayerAvatar, nullptr, &rectDst);

    rectDst.x = position.x + 78 + 15;
    rectDst.y += 10;
    roundedBoxRGBA(renderer, rectDst.x, rectDst.y, rectDst.x + widthMpBar, rectDst.y + heightMpBar, 4,
                   colorMpBarBackground.r, colorMpBarBackground.g, colorMpBarBackground.b, colorMpBarBackground.a);

    rectDst.x += widthBorderMpBar;
    rectDst.y += widthBorderMpBar;
    rectDst.w = widthMpBar - 2 * widthBorderMpBar;
    rectDst.h = heightMpBar - 2 * widthBorderMpBar;
    double process = PlayerManager::Instance().GetCurrentMp() / 100;
    roundedBoxRGBA(renderer, rectDst.x, rectDst.y, rectDst.x + (int) (rectDst.w * process), rectDst.y + rectDst.h,
                   2, colorMpBarForeground.r, colorMpBarForeground.g, colorMpBarForeground.b, colorMpBarForeground.a);
}